package com.web.base.log.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.log.exception.MessageException;
import com.web.base.log.facade.MessageFacade;
import com.web.base.log.message.MessageRequest;
import com.web.base.log.message.MessageResponse;
import com.web.base.log.model.Message;
import com.web.base.log.service.MessageService;
import com.web.base.log.util.MessageConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月20日
 */
@Component("logMessageProvider")
public class MessageProvider implements MessageFacade
{
	@Autowired
	private MessageService messageService;
	
	public MessageProvider()
	{
	}

	@Override
	public MessageResponse add(MessageRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Message message = MessageConvertor.toMessage(request);
		message = messageService.save(message);
		MessageResponse response = MessageConvertor.toResponse(message);
		return response;
	}

	@Override
	public MessageResponse update(MessageRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Message message = MessageConvertor.toMessage(request);
		message = messageService.save(message);
		MessageResponse response = MessageConvertor.toResponse(message);
		return response;
	}

	@Override
	public MessageResponse get(Integer id)
	{
		if (id == null)
			throw new MessageException("Id不能为空");
			
		Message message = messageService.get(id);
		MessageResponse response = MessageConvertor.toResponse(message);
		return response;
	}

	@Override
	public Page<MessageResponse> listPage(MessageRequest request, int start, int size)
	{
		Message message = MessageConvertor.toMessage(request);
		Page<Message> page = new Page<>(size);
		page.setCurPage(start);
		page = messageService.list(message, page);
		Page<MessageResponse> pageResponse = MessageConvertor.toResponse(page);
		return pageResponse;
	}
	
}
